package com.karson.admin.service.Impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.karson.admin.service.LoginService;
import com.karson.common.exception.AsmrException;
import com.karson.common.model.SystemAdmin;
import com.karson.common.request.LoginAdminRequest;
import com.karson.common.response.LoginAdminResponse;
import com.karson.common.response.SystemAdminResponse;
import com.karson.common.util.AsmrUtil;
import com.karson.common.util.SercretUtil;

import com.karson.service.service.SystemAdminService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ua_parser.Client;
import ua_parser.Parser;

@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private SystemAdminService systemAdminService;

    @Override
    public LoginAdminResponse login(LoginAdminRequest loginAdminRequest, HttpServletRequest request) {
        //判断用户名 或者密码是否为空
        if (loginAdminRequest.getAccount() == null || loginAdminRequest.getPassword() == null) {
            throw new AsmrException("账号密码不能为空");
        }
        LambdaQueryWrapper<SystemAdmin> systemAdminWrapper = new LambdaQueryWrapper<>();
        systemAdminWrapper.eq(SystemAdmin::getAccount, loginAdminRequest.getAccount());
        SystemAdmin systemAdmin = systemAdminService.getOne(systemAdminWrapper);
        if (systemAdmin == null) {
            throw new AsmrException("请检查下你的账号呢~");
        }
        String password = SercretUtil.encryptPassword(loginAdminRequest.getPassword(), loginAdminRequest.getAccount());
        if (!password.equals(systemAdmin.getPwd())) {
            throw new AsmrException("您的密码错误了呢~");
        }
        String UserAgent = request.getHeader("User-Agent");
        Parser parser = new Parser();
        Client client = parser.parse(UserAgent);
        String device = JSONUtil.toJsonStr(client.device.family);
        StpUtil.login(systemAdmin.getId(),device);
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        // 更改ip TODO
        LoginAdminResponse loginAdminResponse = new LoginAdminResponse();
        loginAdminResponse.setId(systemAdmin.getId());
        loginAdminResponse.setType(device);
        loginAdminResponse.setToken(tokenInfo.getTokenValue());
        loginAdminResponse.setRealName(systemAdmin.getRealName());
        loginAdminResponse.setPhone(systemAdmin.getPhone());
        return loginAdminResponse;
    }

    @Override
    public void register(LoginAdminRequest loginAdminRequest) {
        if (loginAdminRequest.getAccount() == null || loginAdminRequest.getPassword() == null) {
            throw new AsmrException("账号密码不能为空");
        }
        LambdaQueryWrapper<SystemAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemAdmin::getAccount, loginAdminRequest.getAccount());
        SystemAdmin systemAdmin = systemAdminService.getOne(queryWrapper);
        if (systemAdmin != null) {
            throw new AsmrException("账号已存在");
        }
        String password = SercretUtil.encryptPassword(loginAdminRequest.getPassword(), loginAdminRequest.getAccount());
        systemAdmin = new SystemAdmin();
        systemAdmin.setAccount(loginAdminRequest.getAccount());
        systemAdmin.setPwd(password);
        systemAdmin.setRealName("访客" + loginAdminRequest.getAccount());
        boolean save = systemAdminService.save(systemAdmin);
        if (!save) {
            throw new AsmrException("创建用户失败");
        }
    }

    @Override
    public SystemAdminResponse getAdminInfo() {
        Object loginId = StpUtil.getLoginId();

        LambdaQueryWrapper<SystemAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemAdmin::getId, loginId.toString());
        SystemAdmin systemAdmin = systemAdminService.getOne(wrapper);

        return BeanUtil.copyProperties(systemAdmin, SystemAdminResponse.class);
    }
}
